package cn.com.chaochuang.gxfdaoa.pdfserver;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

/**
 * 2018-1-15
 *
 * @author Shicx
 */
@Component
public class PdfServerSchedule {

    private final Logger logger = LoggerFactory.getLogger(PdfServerSchedule.class);

    @Value("${upload.word.root.path}")
    private String wordRootPath;

    /**
     * 删除当天之前的所有文件,每天1点执行
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void removeFileSchedule(){
        logger.info("--->删除文件夹");
        //word文件临时存放的文件夹
        File wordFolder = new File(wordRootPath);
        if(wordFolder.exists()) {
            //每日产生的文件夹
            File[] folderList = wordFolder.listFiles();
            if(folderList!=null) {
                //当天的文件夹名称
                String folderName = PDFHelper.FOLDER_NAME_FMT.format(new Date());
                for (File folder : folderList) {
                    try {
                        if(!folder.getName().equals(folderName)) {
                            FileUtils.deleteDirectory(folder);
                            logger.info("--->删除文件夹成功");
                        }
                    } catch (IOException e) {
                        logger.error("--->删除文件夹失败！！！");
                        e.printStackTrace();
                    }
                }
            }

        }
    }

}
